package com.smyh.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.smyh.bean.SearchParam;

/**
 * Created by Administrator on 2016/7/8.
 */
public class SearchParamDB extends SQLiteOpenHelper{
    private final static String database_name = "books.db";
    private final static int db_version = 1;

    public final static String table_name ="search_param";
    public final static String key_colum = "ID_";
    public final static String rf_call_time = "rf_call_time";
    public final static String rf_wait_time = "rf_wait_time";
    public final static String rf_call_distance = "rf_call_distance";
    public final static String rf_comm_freq = "rf_comm_freq";
    public final static String rf_call_chanel = "rf_call_chanel";

    /**
     * @param context to use to open or create the database
     */
    public SearchParamDB(Context context ) {
        super(context, null, null, db_version);
    }

    /**
     * @param context      to use to open or create the database
     * @param name         of the database file, or null for an in-memory database
     * @param factory      to use for creating cursor objects, or null for the default
     * @param version      number of the database (starting at 1); if the database is older,
     *                     {@link #onUpgrade} will be used to upgrade the database; if the database is
     *                     newer, {@link #onDowngrade} will be used to downgrade the database
     * @param errorHandler the {@link DatabaseErrorHandler} to be used when sqlite reports database
     */
    public SearchParamDB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    /**
     *
     * @param db The database.
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table if not exists "+table_name+
                " ("+ key_colum + " int primary key not null,"+rf_call_time+" int,"+rf_wait_time+" int," +
                rf_call_distance + " int,"+rf_comm_freq+" int,"+rf_call_chanel+" int)";
        db.execSQL(sql);
    }

    /**
      *
     * @param db         The database.
     * @param oldVersion The old database version.
     * @param newVersion The new database version.
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void updateParam(ContentValues cv){
        SQLiteDatabase rdDB = this.getReadableDatabase();
        String[] selectParam = {"1"};
        Cursor cs = rdDB.query(table_name,null,key_colum+"=?",selectParam,null,null,null);
        int count = cs.getCount();
        SQLiteDatabase wtDB = this.getWritableDatabase();
        if(count == 1){
            wtDB.update(table_name,cv,key_colum+"=?",selectParam);
        }else{
            wtDB.insert(table_name,null,cv);
        }
    }

    public SearchParam selectParamByID(){
        SQLiteDatabase db = this.getReadableDatabase();
        String[] selectParam = {"1"};
        Cursor cs = db.query(table_name,null,key_colum+"=?",selectParam,null,null,null);
        if(cs.getCount() == 1){
            SearchParam param = new SearchParam();

            param.setId_(cs.getInt(0));
            param.setRf_call_time(cs.getInt(1));
            param.setRf_wait_time(cs.getInt(2));
            param.setRf_call_distance(cs.getInt(3));
            param.setRf_comm_freq(cs.getInt(4));
            param.setRf_call_chanel(cs.getInt(5));

            return param;
        }
        return null;
    }
}
